package switchviz.graphic.gauge.graph2d;

import javax.swing.Box;

import org.jdom.Element;

import switchviz.core.ResourceManager;
import switchviz.graphic.IGraphicsPanel;
import switchviz.graphic.gauge.graph2d.Graph2dDataModel.AlarmMode;
import switchviz.graphic.gauge.graph2d.Graph2dDataModel.ScaleMode;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;

public class Graph2dPanel extends IGraphicsPanel
{
	
	private static final long serialVersionUID = -1184858356222921120L;
	private Graph2dPersistence _xmlhandler = null;
	
	private JMaskedTextField _txtdatamin = null;
	private JMaskedTextField _txtdatamax = null;
	private JMaskedTextField _txtpointsize = null;
	private JMaskedTextField _txtdatalength = null;
	private JMaskedTextField _txtgranularity = null;
	private JLabeldComponent _txtname = null;
	private JLabeldComponent _cmbalarmmode = null;
	private JLabeldComponent _cmbscalemode = null;
	private JLabeldComponent _cbmultiport = null;
	
	public Graph2dPanel(Element root)
	{
		super();
		initialize();
		
		setElement(root);
	}
	
	private void initialize()
	{
		_cbmultiport = new JLabeldComponent("Multiple per Port: ", JLabeldComponent.CHECKBOX);
		_txtdatamin = new JMaskedTextField("Lower Limit: ", JMaskedTextField.FLOATNUM);
		_txtdatamax = new JMaskedTextField("Upper Limit: ", JMaskedTextField.FLOATNUM);
		_txtname = new JLabeldComponent("Name: ", JLabeldComponent.TEXTFIELD);
		_txtpointsize = new JMaskedTextField("Point Size: ", JMaskedTextField.FLOATNUM);
		_txtdatalength = new JMaskedTextField("Data Length: ", JMaskedTextField.FLOATNUM);
		_txtgranularity = new JMaskedTextField("Granularity: ", JMaskedTextField.INTNUM);
		_cmbalarmmode = new JLabeldComponent("Alarm Mode: ", JLabeldComponent.COMBOBOX);
		_cmbalarmmode.getComboBox().addItem("None");
		_cmbscalemode = new JLabeldComponent("Scalemode: ", JLabeldComponent.COMBOBOX);
		_cmbscalemode.getComboBox().addItem("Linear");
		_cmbscalemode.getComboBox().addItem("Logarithm");
		this.add(_txtname);
		this.add(_cbmultiport);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_cmbscalemode);
		this.add(_txtdatamin);
		this.add(_txtdatamax);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_txtdatalength);
		this.add(_txtgranularity);
		this.add(_txtpointsize);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_cmbalarmmode);
	}
	
	public Element getElement()
	{
		Graph2dDataModel datamodel = new Graph2dDataModel();
		
		datamodel.setDataMin(Float.valueOf(_txtdatamin.getText()));
		datamodel.setDataMax(Float.valueOf(_txtdatamax.getText()));
		datamodel.setPointsize(Float.valueOf(_txtpointsize.getText()));
		datamodel.setDataLength(Integer.valueOf(_txtdatalength.getText()));
		datamodel.setGranularity(Integer.valueOf(_txtgranularity.getText()));
		datamodel.setName(_txtname.getText());
		datamodel.setMultiplePerPort(_cbmultiport.getCheckBox().isSelected());
		datamodel.setType(this.getName());
		
		if (_cmbalarmmode.getText().equals("None"))
			datamodel.setAlarmMode(AlarmMode.NONE);
		
		if (_cmbscalemode.getText().equals("Linear"))
			datamodel.setScaleMode(ScaleMode.LINEAR);
		else if (_cmbscalemode.getText().equals("Logarithm"))
			datamodel.setScaleMode(ScaleMode.LOGARITHM);
		
		return _xmlhandler.getElement(datamodel);
	}
	
	public void setElement(Element node)
	{
		_xmlhandler = new Graph2dPersistence(node);
		
		Graph2dDataModel datamodel = _xmlhandler.getDataModel(node);
		
		_txtdatamin.setText(String.valueOf(datamodel.getDataMin()));
		_txtdatamax.setText(String.valueOf(datamodel.getDataMax()));
		_txtpointsize.setText(String.valueOf(datamodel.getPointsize()));
		_txtdatalength.setText(String.valueOf(datamodel.getDataLength()));
		_txtgranularity.setText(String.valueOf(datamodel.getGranularity()));
		_txtname.setText(datamodel.getName());
		_cbmultiport.getCheckBox().setSelected(datamodel.isMultiplePerPort());
		
		this.setName(datamodel.getType());
		
		switch (datamodel.getAlarmMode())
		{
		case NONE:
			_cmbalarmmode.setText("None");
			break;
		}
		
		switch(datamodel.getScaleMode())
		{
		case LINEAR:
			_cmbscalemode.setText("Linear");
			break;
		case LOGARITHM:
			_cmbscalemode.setText("Logarithm");
			break;
		}
	}
}